﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Diagnostics;
using System.Web.Configuration;
using System.Configuration;
using Newtonsoft.Json;
using System.Security.Principal;
using CloudStorageLight.Web.Models;
using CloudStorageLight.Core;
using System.IO.Compression;
using System.Threading;
using CloudStorageLight.Core.Web;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity;
using Microsoft.WindowsAzure.Storage.Table;
using ElCamino.AspNet.Identity.AzureTable.Model;
using System.Text.RegularExpressions;
using System.Web.Caching;

namespace CloudStorageLight.Web.Controllers
{
    /// <summary>
    /// FilesControllerのヘルパー
    /// </summary>
    [Authorize]
    public class HelperController : HelperControllerBase
    {
        public ApplicationUserManager UserManager
        {
            get
            {
                _userManager = _userManager ?? OwinContextExtensions.GetUserManager <ApplicationUserManager>(HttpContext.GetOwinContext());
                return _userManager;
            }
            private set
            {
                _userManager = value;
            }
        }
        private ApplicationUserManager _userManager;


        protected override BlobService CreateBlobService()
        {
            return new BlobService();
        }

        protected override BlobUser GetCurrentUser()
        {
            if (_user == null)
            {
                _user = BasicAuthenticateUtil.GetCurrentUser();
            }
            return _user;
        }
        private BlobUser _user;

        [AllowAnonymous]
        public ActionResult Error()
        {
            return View();
        }

        public ActionResult GetCloudAccount()
        {
            var account = BlobService.GetCurrentCloudAccount();
            return Json(new { Account = account.AccountName, account.AccountType, account.EMailAddress, account.SpHostUrl }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UserRoleList(string term)
        {
            var cloudAccount = BlobService.GetCurrentCloudAccount();
            var cacheKey = cloudAccount.AccountName + "_UserRoleList";
            List<string> items = this.HttpContext.Application[cacheKey] as List<string>;
            if (items == null) items = new List<string>();

            var filteredItems = items.Where(
                item => item.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0
                ).Take(200);
            return Json(filteredItems, JsonRequestBehavior.AllowGet);
        }


        [AllowAnonymous]
        public ActionResult SelfTest(string cloudAccount, string userName, string password, string testPath)
        {
            this.ViewBag.CloudAccount = cloudAccount;
            this.ViewBag.UserName = userName;
            this.ViewBag.Password = password;
            this.ViewBag.TestPath = testPath;
            return View();
        }

    }

}